package com.work.straighttalk.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.hyphenate.chat.EMMessage;
import com.work.straighttalk.R;
import com.work.straighttalk.adapter.ConversationRVAdapter;
import com.work.straighttalk.adapter.OnRecyclerViewClickListener;
import com.work.straighttalk.app.BaseFragment;
import com.work.straighttalk.bean.MyConversation;
import com.work.straighttalk.presenter.ConversationLoadDataPImpl;
import com.work.straighttalk.presenter.i.LoadDataPresenterI;
import com.work.straighttalk.ui.ChatActivity;
import com.work.straighttalk.ui.GroupBuilderActivity;
import com.work.straighttalk.ui.i.BindDataVI;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by dllo on 16/3/1.
 */
public class ConversationFragment extends BaseFragment implements BindDataVI<List<MyConversation>>, OnRecyclerViewClickListener {
    @Bind(R.id.rv_conversation)
    RecyclerView rvConversation;
    private ConversationRVAdapter adapter;
    private LoadDataPresenterI presenter;
    private static int REQUEST_BUILD_GROUP;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_conversation, container, false);
        setHasOptionsMenu(true);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.main_toolbar_contacts, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.build_new_group:
                Intent intent = new Intent(getContext(), GroupBuilderActivity.class);
                getActivity().startActivityForResult(intent, REQUEST_BUILD_GROUP);
                break;
        }
        return false;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        adapter = new ConversationRVAdapter();
        rvConversation.setLayoutManager(new LinearLayoutManager(getContext()));
        rvConversation.setAdapter(adapter);
        adapter.setListener(this);
        presenter = new ConversationLoadDataPImpl(this);
        presenter.loadData(null);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    @Override
    public void bindData(List<MyConversation> myConversations) {
        adapter.loadData(myConversations);
    }

    public void refresh() {
        adapter.notifyDataSetChanged();
    }


    @Override
    public void onRecyclerViewCLick(Object o) {
        String userID = (String) o;
        Intent intent = new Intent(getContext(), ChatActivity.class);
        intent.putExtra("userID", userID);
        startActivity(intent);
    }


}
